﻿<# 
.SYNOPSIS 
    Clears the IPC resources of the PeopleSoft Application Server.
.DESCRIPTION 
    Clears the IPC resources for the specified PeopleSoft Application Server domain.
    This command will require the domain to be shut down.
.NOTES 
    File Name  : Clear-PsftIPC.ps1 
    Author     : Scott Kearney - scott@kearney.tk 
    Requires   : PowerShell V3 
.EXAMPLE 
    Clear-PsftIPC -PS_HOME C:\psoft\PS_HOME -PS_CFG_HOME C:\psoft\PS_CFG_HOME -ServerType AppServer -Domain DOMAIN1
    Clears the IPC resources of DOMAIN1.
.PARAMETER PS_HOME
    The full path to the PS_HOME directory for this domain.
.PARAMETER PS_CFG_HOME
    The full path to the PS_CFG_HOME directory for this domain.
.PARAMETER ServerType
    The type of server on which to perform the action.
.PARAMETER Domain
    The name of the domain on which to perform the action.
#> 
function Clear-PsftIPC
{
    [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='High')]
    Param
    (
        # The full path to the PS_HOME directory for this domain.
        # See http://docs.oracle.com/cd/E41633_01/pt853pbh1/eng/pt/tsvt/concept_UnderstandingPS_HOMEandPS_CFG_HOME-eb7ece.html
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The full path to the PS_HOME directory for this domain.",
                   Position=0)]
        [ValidateScript({Validate_PS_HOME $_})]
        [String]
        $PS_HOME,

        # The full path to the PS_CFG_HOME directory for this domain.
        # See http://docs.oracle.com/cd/E41633_01/pt853pbh1/eng/pt/tsvt/concept_UnderstandingPS_HOMEandPS_CFG_HOME-eb7ece.html
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The full path to the PS_CFG_HOME directory for this domain.",
                   Position=1)]
        [ValidateScript({Validate_PS_CFG_HOME $_})]
        [String]
        $PS_CFG_HOME,

        # The type of server on which to perform the action.
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The type of server on which to perform the action.",
                   Position=2)]
        [ValidateSet("AppServer","PrcsScheduler","SearchServer")]
        $ServerType,

        # The name of the domain on which to perform the action.
        # Should be a directory under the PS_CFG_HOME
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The name of the domain on which to perform the action.",
                   Position=3)]
        [ValidateScript({Validate_Domain $PS_CFG_HOME $ServerType $_})]
        [String]
        $Domain
    )

    Begin
    {
        function Clear_PSAppServerIPC{
            param($PS_HOME,$PS_CFG_HOME,$Domain)

            
        }
    }
    Process
    {
        # Set up variables
        $return = ""

        # Ensure that the user would like us to perform this potentially destructive action.
        if (($pscmdlet.ShouldProcess("$Domain", "Clear-PsftIPC")))
        {
            Write-Verbose "Confirmed to clear the IPC resources."

            # If the domain is running, cleaning IPC resources will inherently shut down the domain (forcibly).
            if(Get-PsftStatus -PS_HOME $PS_HOME -PS_CFG_HOME $PS_CFG_HOME -ServerType $ServerType -Domain $Domain -Target Server)
            {
                Write-Verbose "The application server is running. Confirm that the application server will be shut down."

                # Confirm that the user understands that cleaning the IPC resources of a domain will shut down the domain.
                if (!($pscmdlet.ShouldProcess("$Domain", "Stop-PS")))
                {
                    throw "The Application Server must be shut down to clear the IPC resources of the domain $Domain."
                }
            }

            Write-Verbose "Clearing IPC resources..."

            # Invokes PSAdmin.exe to clear the IPC resources of the specified domain.
            $PSAdmin = Invoke-PSAdmin -PS_HOME $PS_HOME -PS_CFG_HOME $PS_CFG_HOME -ServerType $ServerType -Domain $Domain -Command "cleanipc"

            $result = $PSAdmin.Result

            # Retrieve line numbers for certain markers in the resulting text.
            $MessageQueuesLineNumber = $result | Where {$_.GetType() -match "String"} | Select-String "Message Queues" | Select -ExpandProperty LineNumber
            $SharedMemoryLineNumber  = $result | Where {$_.GetType() -match "String"} | Select-String "Shared Memory"  | Select -ExpandProperty LineNumber
            $SemaphoresLineNumber    = $result | Where {$_.GetType() -match "String"} | Select-String "Semaphores"     | Select -ExpandProperty LineNumber
            $RemovingIPCLineNumber   = $result | Where {$_.GetType() -match "String"} | Select-String "Removing IPC resources..." | Select -ExpandProperty LineNumber

            # Parse the text and return the relevant data.
            $return = New-Object -TypeName Object |
                        Add-Member -MemberType NoteProperty -Name MessageQueues -Value ([int[]]@($result | Where {$_.GetType() -match "String"})[$MessageQueuesLineNumber..$($SharedMemoryLineNumber-3)]) -PassThru | 
                        Add-Member -MemberType NoteProperty -Name SharedMemory  -Value ([int[]]@($result | Where {$_.GetType() -match "String"})[$SharedMemoryLineNumber..$($SemaphoresLineNumber-3)]) -PassThru |
                        Add-Member -MemberType NoteProperty -Name Semaphores    -Value ([int[]]@($result | Where {$_.GetType() -match "String"})[$SemaphoresLineNumber..$($RemovingIPCLineNumber-3)]) -PassThru
        
        }

        return $return
    }
    End
    {
    }
}

